package com.dandelion.common.utils;


import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class CommonUtils {
    public static <T> T selectOne(List<T> list) {
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public static final boolean toBool(Byte value) {
        return value != null && value.intValue() == 1;
    }

    public static <T> void addIf(Collection<T> list, Predicate<T> predicate, T... appendArr) {
        Stream.of(appendArr).forEach(t -> {
            if (predicate.test(t)) {
                list.add(t);
            }
        });
    }

    public static void addIfUnsignedInt(Collection<Integer> list, Integer value) {
        if (value != null && value > 0) {
            list.add(value);
        }
    }
}
